package com.vm.myview;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.vm.activity.DetailActivity;
import com.vm.model.UpgradeSoft;
import com.wwtscapp.apk.R;

/**
 * 
 * @ProjectName: [WeiMai]
 * @Author: [kent.wang]
 * @CreateDate: [2015年3月27日 下午2:17:00]
 * @Version: [v1.0]
 * 
 */
public class ViewUpgradeUtils {

	private Context mContext;
	Activity activity;

	public ViewUpgradeUtils(Context context, Activity activity) {
		this.mContext = context;
		this.activity = activity;
	}

	/**
	 * 动态创建LinearLayout
	 * 
	 * @param i
	 * @param upgradeSoft
	 * @param progress
	 *            1.初始化进度条,0不初始化进度条
	 * @return
	 */
	public LinearLayout dyAddView(int i, final UpgradeSoft upgradeSoft,
			int progress) {

		LinearLayout linearLayout = (LinearLayout) activity.getLayoutInflater()
				.inflate(R.layout.software_update_item, null);

		TextView tv_softName = (TextView) linearLayout
				.findViewById(R.id.tv_softName);
		ImageView iv_softImage = (ImageView) linearLayout
				.findViewById(R.id.iv_softImage);
		TextView tv_softSize = (TextView) linearLayout
				.findViewById(R.id.tv_softSize);
		TextView tv_old_version_number = (TextView) linearLayout
				.findViewById(R.id.tv_old_version_number);
		TextView tv_new_version_number = (TextView) linearLayout
				.findViewById(R.id.tv_new_version_number);

		if (upgradeSoft.getName() != null) {
			// String softName =
			// StringUtil.getShotName(soft.getName());
			tv_softName.setText(upgradeSoft.getName());
		}
		// 大小
		if (upgradeSoft.getSize() != 0) {
			float size = upgradeSoft.getSize();
			tv_softSize.setText(Float.toString(size) + "M");
		}

		if (upgradeSoft.getCurrentVersion() != null) {
			tv_old_version_number.setText(upgradeSoft.getCurrentVersion());
		}

		if (upgradeSoft.getNewVersion() != null) {
			tv_new_version_number.setText(upgradeSoft.getNewVersion());
		}

		if (upgradeSoft.getPackageName() != null) {
			try {
				iv_softImage.setImageDrawable(mContext.getPackageManager()
						.getApplicationIcon(upgradeSoft.getPackageName()));
			} catch (NameNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		OnClickListener ll_listener = new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(activity, DetailActivity.class);

				intent.putExtra("softId", upgradeSoft.getSoftId());
				activity.startActivityForResult(intent, 10);
			}
		};

		linearLayout.setOnClickListener(ll_listener);

		new ProgressDown(mContext, linearLayout, upgradeSoft);

		// downloadService.initProgressBar(soft);
		return linearLayout;
	}

}
